package com.szholly.data.general;

import java.util.List;

import com.szholly.utils.util.StringUtils;
import com.szholly.data.cache.IDatabaseCache;
import com.szholly.data.cache.SimpleDatabaseCache;
import com.szholly.data.common.util.ConvertCaptionUtils;

/**
 * 定义提供主动的数据缓存数据表类
 */
public abstract class CachedTableBase extends TableBase {

	protected IDatabaseCache dataCache;

	public CachedTableBase() {
		dataCache = new SimpleDatabaseCache();
	}

	/**
	 * 执行SQL语句返回查询结果object
	 * 
	 * @param queryFilter
	 * @return
	 */
	@Override
	public Object getExecuteScalar(IQueryFilter queryFilter) {
		String key = "getExecuteScalar";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			Object row = super.getExecuteScalar(queryFilter);
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return obj;
		}
	}

	/**
	 * 执行SQL语句返回查询结果object
	 * 
	 * @param queryFilter
	 * @return
	 */
	@Override
	public Object getExecuteScalar(IQueryMap queryFilter) {
		String key = "getExecuteScalar";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			Object row = super.getExecuteScalar(queryFilter);
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return obj;
		}
	}

	/**
	 * 执行SQL语句返回查询结果object
	 * 
	 * @param queryFilter
	 * @return
	 */
	@Override
	public Object getExecuteScalar(RowBase queryFilter) {
		String key = "getExecuteScalar";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			Object row = super.getExecuteScalar(queryFilter);
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return obj;
		}
	}

	/**
	 * 将数据绑定到自定义数据类型上
	 * 
	 * @param queryFilter
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> T getEntity(Class<T> c, IQueryFilter queryFilter) {
		String key = "getEntity";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			T row = super.getEntity(c, queryFilter);
			if (queryFilter != null && queryFilter.getAddCaptionField()) {
				ConvertCaptionUtils.convertCaptionField(this,
						queryFilter.getSelectFields(), row);
			}
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return (T) obj;
		}
	}

	/**
	 * 将数据绑定到自定义数据类型上
	 * 
	 * @param queryFilter
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> T getEntity(Class<T> c, IQueryMap queryFilter) {
		String key = "getEntity";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			T row = super.getEntity(c, queryFilter);
			if (queryFilter != null && queryFilter.getAddCaptionField()) {
				ConvertCaptionUtils.convertCaptionField(this,
						queryFilter.getSelectFields(), row);
			}
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return (T) obj;
		}
	}

	/**
	 * 将数据绑定到自定义数据类型上
	 * 
	 * @param queryFilter
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> T getEntity(Class<T> c, RowBase queryFilter) {
		String key = "getEntity";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			T row = super.getEntity(c, queryFilter);
			ConvertCaptionUtils.convertCaptionField(this, null, row);
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return (T) obj;
		}
	}

	/**
	 * 将数据绑定到自定义数据类型上
	 * 
	 * @param queryFilter
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> List<T> getEntityList(Class<T> c,
			IQueryFilter queryFilter) {
		String key = "getEntityList";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			List<T> row = super.getEntityList(c, queryFilter);
			if (queryFilter != null && queryFilter.getAddCaptionField()) {
				ConvertCaptionUtils.convertCaptionField(this,
						queryFilter.getSelectFields(), row);
			}
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return (List<T>) obj;
		}
	}

	/**
	 * 将数据绑定到自定义数据类型上
	 * 
	 * @param queryFilter
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> List<T> getEntityList(Class<T> c,
			IQueryMap queryFilter) {
		String key = "getEntityList";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			List<T> row = super.getEntityList(c, queryFilter);
			if (queryFilter != null && queryFilter.getAddCaptionField()) {
				ConvertCaptionUtils.convertCaptionField(this,
						queryFilter.getSelectFields(), row);
			}
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return (List<T>) obj;
		}
	}

	/**
	 * 将数据绑定到自定义数据类型上
	 * 
	 * @param queryFilter
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> List<T> getEntityList(Class<T> c,
			RowBase queryFilter) {
		String key = "getEntityList";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			List<T> row = super.getEntityList(c, queryFilter);
			ConvertCaptionUtils.convertCaptionField(this, null, row);
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return (List<T>) obj;
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param queryFilter
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> List<T> pageSelect(Class<T> c,
			IQueryFilter queryFilter, PageInfo pageInfo) {
		String key = "pageSelect";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		key += "_" + pageInfo.getKey();
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		String rowcountKey = key + "rowcount";
		
		if (obj == null) {
			List<T> list = super.pageSelect(c, queryFilter, pageInfo);
			if (queryFilter != null && queryFilter.getAddCaptionField()) {
				ConvertCaptionUtils.convertCaptionField(this,
						queryFilter.getSelectFields(), list);
			}
			dataCache.setCache(this.getMetadataTableName(), key, list);
			dataCache.setCache(this.getMetadataTableName(), rowcountKey, pageInfo.getRowCount());
			
			return list;
		} else {
			int rowcount = (int)dataCache.getCache(this.getMetadataTableName(), rowcountKey);
			pageInfo.setRowCount(rowcount);
			
			return (List<T>) obj;
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param queryFilter
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> List<T> pageSelect(Class<T> c,
			IQueryMap queryFilter, PageInfo pageInfo) {
		String key = "pageSelect";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		key += "_" + pageInfo.getKey();
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		String rowcountKey = key + "rowcount";
		
		if (obj == null) {
			List<T> list = super.pageSelect(c, queryFilter, pageInfo);
			if (queryFilter != null && queryFilter.getAddCaptionField()) {
				ConvertCaptionUtils.convertCaptionField(this,
						queryFilter.getSelectFields(), list);
			}
			dataCache.setCache(this.getMetadataTableName(), key, list);
			dataCache.setCache(this.getMetadataTableName(), rowcountKey, pageInfo.getRowCount());
			
			return list;
		} else {
			int rowcount = (int)dataCache.getCache(this.getMetadataTableName(), rowcountKey);
			pageInfo.setRowCount(rowcount);
			
			return (List<T>) obj;
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param queryFilter
	 * @param pageInfo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> List<T> pageSelect(Class<T> c,
			RowBase queryFilter, PageInfo pageInfo) {
		String key = "pageSelect";
		if (queryFilter == null) {
			key += "_null";
		} else {
			key += "_" + queryFilter.getKey();
		}
		key += "_" + pageInfo.getKey();
		String rowcountKey = key + "rowcount";
		
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			List<T> list = super.pageSelect(c, queryFilter, pageInfo);
			ConvertCaptionUtils.convertCaptionField(this, null, list);
			dataCache.setCache(this.getMetadataTableName(), key, list);
			dataCache.setCache(this.getMetadataTableName(), rowcountKey, pageInfo.getRowCount());
			
			return list;
		} else {
			int rowcount = (int)dataCache.getCache(this.getMetadataTableName(), rowcountKey);
			pageInfo.setRowCount(rowcount);
			
			return (List<T>) obj;
		}
	}

	/**
	 * 获取某列的最大值
	 * 
	 * @param fieldName
	 * @param otherCondition
	 * @param paras
	 * @return
	 */
	@Override
	public long getSpecFieldMaxValue(String fieldName, String otherCondition,
			GlobalParameter[] paras) {
		String key = "getSpecFieldMaxValue";
		key += "_" + fieldName;
		key += "_" + otherCondition;
		key += "_" + QueryFilter.getParaKey(paras);
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			long row = super.getSpecFieldMaxValue(fieldName, otherCondition,
					paras);
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return (Long) obj;
		}
	}

	/**
	 * 获取某列的最小值
	 * 
	 * @param fieldName
	 * @param otherCondition
	 * @param paras
	 * @return
	 */
	@Override
	public long getSpecFieldMinValue(String fieldName, String otherCondition,
			GlobalParameter[] paras) {
		String key = "getSpecFieldMinValue";
		key += "_" + fieldName;
		key += "_" + otherCondition;
		key += "_" + QueryFilter.getParaKey(paras);
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			long row = super.getSpecFieldMinValue(fieldName, otherCondition,
					paras);
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return (Long) obj;
		}
	}

	/**
	 * 获取下一个要素，如果没有返回null
	 * 
	 * @param t
	 * @param byFieldName
	 * @param whereString
	 * @param orderByType
	 * @param addCaptionField
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> T getNextEntity(Class<T> c, RowBase t,
			String byFieldName, String whereString, OrderByType orderByType,
			boolean addCaptionField) {
		String key = "getNextEntity";
		key += "_" + (String) t.getValue(GlobalDataType.String, byFieldName);
		key += "_" + byFieldName;
		if (StringUtils.IsNullOrSpace(whereString)) {
			key += "_" + "null";
		} else {
			key += "_" + whereString;
		}
		key += "_" + orderByType.toString();
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			T row = super.getNextEntity(c, t, byFieldName, whereString,
					orderByType, addCaptionField);
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return (T) obj;
		}
	}

	/**
	 * 获取上一个要素，如果没有返回null
	 * 
	 * @param t
	 * @param byFieldName
	 * @param whereString
	 * @param orderByType
	 * @param addCaptionField
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public <T extends RowBase> T getPerEntity(Class<T> c, RowBase t,
			String byFieldName, String whereString, OrderByType orderByType,
			boolean addCaptionField) {
		String key = "getPerEntity";
		key += "_" + (String) t.getValue(GlobalDataType.String, byFieldName);
		key += "_" + byFieldName;
		if (StringUtils.IsNullOrSpace(whereString)) {
			key += "_" + "null";
		} else {
			key += "_" + whereString;
		}
		key += "_" + orderByType.toString();
		Object obj = dataCache.getCache(this.getMetadataTableName(), key);
		if (obj == null) {
			T row = super.getPerEntity(c, t, byFieldName, whereString,
					orderByType, addCaptionField);
			dataCache.setCache(this.getMetadataTableName(), key, row);
			return row;
		} else {
			return (T) obj;
		}
	}

	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public void save(RowBase entity) {
		super.save(entity);
		dataCache.clearCache(this);
	}

	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public <T extends RowBase> void save(List<T> entity) {
		super.save(entity);
		dataCache.clearCache(this);
	}

	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public void insert(RowBase entity) {
		super.insert(entity);
		dataCache.clearCache(this);
	}

	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public <T extends RowBase> void insert(List<T> entity) {
		super.insert(entity);
		dataCache.clearCache(this);
	}

	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public void update(RowBase entity) {
		super.update(entity);
		dataCache.clearCache(this);
	}

	/**
	 * 更新或添加行
	 * 
	 * @param entity
	 * @return
	 */
	@Override
	public <T extends RowBase> void update(List<T> entity) {
		super.update(entity);
		dataCache.clearCache(this);
	}

	/**
	 * 更新数据
	 * 
	 * @param updateFilter
	 */
	@Override
	public void update(IUpdateFilter updateFilter) {
		super.update(updateFilter);
		dataCache.clearCache(this);
	}

	/**
	 * 更新数据
	 * 
	 * @param updateFilter
	 */
	@Override
	public void update(IUpdateMap updateFilter) {
		super.update(updateFilter);
		dataCache.clearCache(this);
	}

	/**
	 * 删除表记录
	 * 
	 * @param queryFilter
	 */
	@Override
	public void delete(IQueryFilter queryFilter) {
		super.delete(queryFilter);
		dataCache.clearCache(this);
	}

	/**
	 * 删除表记录
	 * 
	 * @param queryFilter
	 */
	@Override
	public void delete(IQueryMap queryFilter) {
		super.delete(queryFilter);
		dataCache.clearCache(this);
	}

	/**
	 * 删除记录行
	 * 
	 * @param row
	 */
	@Override
	public void delete(RowBase row) {
		super.delete(row);
		dataCache.clearCache(this);
	}
}
